package com.atwork.wuhua.utils;

import android.content.Context;
import android.content.Intent;

import com.atwork.wuhua.base.BaseReq;
import com.atwork.wuhua.base.MyApplication;
import com.atwork.wuhua.bean.UserBean;
import com.atwork.wuhua.ui.activity.LoginActivity;


public class UserUtil {


    //登陆成功
    public static void loginSuccess(Context context, final UserBean userBean) {
        //保存信息
        setUserBean(context, userBean);
        // 友盟登录
        MyApplication.userBean = userBean;
        MyApplication.UUID = userBean.getData().getUuid();
        MyApplication.AUTH = userBean.getData().getAuth();
    }

    //保存用户类
    public static void setUserBean(Context context, UserBean userBean) {
        SPUtils.setObject(context, SPUtils.USER_OBJECT, userBean);
    }

    //请求如果有登录 有两个os_open_id os_access_token 系统级参数
    public static void getTokenAndOpenid(Context context, BaseReq map) {
        UserBean userBean = getUserBean(context);
        if (userBean == null) {
            return;
        }
//        map.setKey("os_open_id", userBean.getOpen_id());
//        map.setKey("os_access_token", userBean.getAccess_token());
    }

    //获得用户类
    public static UserBean getUserBean(Context context) {
        return SPUtils.getObject(context, SPUtils.USER_OBJECT, UserBean.class);
    }

    //判断是否登录，未登陆直接打开登陆界面
    public static boolean isLoginOrOpen(Context context) {
        if (!isLogin(context)) {
            Intent intent = new Intent(context, LoginActivity.class);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
                    | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            context.startActivity(intent);
            return false;
        }
        return true;
    }

    //是否登录
    public static boolean isLogin(Context context) {
        UserBean userBean = SPUtils.getObject(context, SPUtils.USER_OBJECT, UserBean.class);
        if (userBean != null) {
            return true;
        }
        return false;
    }

    //退出登录
    public static void loginOut(Context context) {
        //移除信息
        SPUtils.remove(context, SPUtils.USER_OBJECT);
    }

    //获取用户头像
    public static String getHeadIcon(Context context) {
        UserBean userModel = SPUtils.getObject(context, SPUtils.USER_OBJECT, UserBean.class);
        return userModel.getData().getAvatar();
    }

    //获取用户名
    public static String getNickname(Context context) {
        UserBean userModel = SPUtils.getObject(context, SPUtils.USER_OBJECT, UserBean.class);
        return userModel.getData().getNickname();
    }

    //获取请求头
    public static String getAuth(Context context) {
        UserBean userModel = SPUtils.getObject(context, SPUtils.USER_OBJECT, UserBean.class);
        return userModel.getData().getAuth();
    }


}
